package com.example.communitypro.service.impl;

import com.example.communitypro.mapper.*;
import com.example.communitypro.pojo.Comment;
import com.example.communitypro.pojo.Post;
import com.example.communitypro.pojo.UserInformation;
import com.example.communitypro.pojo.UserReport;
import com.example.communitypro.service.CommunityInformationService;
import com.example.communitypro.service.UserReportService;
import com.example.communitypro.util.SpringUtil;
import com.example.communitypro.util.report.DealReport;
import com.example.communitypro.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserReportServiceImpl implements UserReportService {
    @Autowired(required=false)
    private UserReportMapper userReportMapper;
    @Autowired(required = false)
    private UserInformationMapper userInformationMapper;
    @Autowired(required = false)
    private PostMapper postMapper;
    @Autowired(required = false)
    private CommentMapper commentMapper;
    @Override
    public Result addUserReport(UserReport record){
        try {
            UserReport userReport = userReportMapper.judgeReport(record.getUserNo(),record.getReportObjectNo(),record.getReportObjectType());
           if (userReport==null) {
               if (userReportMapper.insertSelective(record) > 0) {
                   if (record.getReportObjectType() == 0) {
                       UserInformation userInformation = userInformationMapper.selectByPrimaryKey(record.getReportObjectNo());
                       int now = userReportMapper.UserReported(record.getReportObjectNo());
                       if (now  >= 10) {
                           userInformation.setUserStatus(0);
                           userInformationMapper.updateByPrimaryKeySelective(userInformation);
                       }
                   }
                   if (record.getReportObjectType() == 1) {
                       Post post = postMapper.selectByPrimaryKey(record.getReportObjectNo());
                       int now = userReportMapper.PostReported(record.getReportObjectNo());
                       System.out.println(now);
                       if (now  >= 10) {
                           post.setPostStatus(0);
                           postMapper.updateByPrimaryKeySelective(post);
                       }
                   }
                   if (record.getReportObjectType() == 2) {
                       Comment comment = commentMapper.selectByPrimaryKey(record.getReportObjectNo());
                       int now = userReportMapper.CommentReported(record.getReportObjectNo());
                       if (now  >= 10) {
                           comment.setCommentStatus(0);
                           commentMapper.updateByPrimaryKeySelective(comment);
                       }
                   }

                   /**
                    * lsl
                    */
                   ApplicationContext ctx = SpringUtil.getApplicationContext();
                   DealReport dealReport = (DealReport) ctx.getBean("dealReport");
                   dealReport.dealReport();

                   return Result.ok().message("新增用户举报成功");
               }
               else{
                   return Result.error().message("新增用户举报失败");
               }
           }
           else {
               System.out.println("已经举报过");
               return Result.error().message("已经举报过");
           }

        }catch (Exception e){
            e.printStackTrace();
            return Result.error().message("新增用户举报失败");
        }
    }


    @Override
    public Result dropUserReport(int UserReportNo){
        try {
            if(userReportMapper.deleteByPrimaryKey(UserReportNo)>0)
            {
                return Result.ok().message("删除用户举报成功");
            }
            else{
                return Result.error().message("删除用户举报失败");
            }
        }catch (Exception e){
            return Result.error().message("删除用户举报失败");
        }
    }

    @Override
    public Result modifyUserReport(UserReport record){
        try {
            if(userReportMapper.updateByPrimaryKeySelective(record)>0)
            {
                return Result.ok().message("修改成功");
            }
            else{
                return Result.error().message("修改失败：未找到对应记录");
            }
        }catch (DuplicateKeyException e){
            System.err.println(e);
            return Result.error().message("修改失败：已经存在对应记录");
        }
        catch (Exception e){
            System.err.println(e);
            return Result.error().message("修改失败："+e.toString());
        }
    }

    @Override
    public Result queryUserReport(int reportNo){
        try {
            UserReport userReport=userReportMapper.selectByPrimaryKey(reportNo);
            if(userReport!=null)
            {
                return Result.ok().message("查找用户举报成功").data("userReport",userReport);
            }
            else{
                return Result.error().message("查找用户举报失败");
            }
        }catch (Exception e){
            return Result.error().message("查找用户举报失败");
        }
    }

    @Override
    public Result queryUserInformationReport(int user_no){
        try {
            List<UserInformation> UserInformations=userReportMapper.getUserInformationReport(user_no);
            if(UserInformations!=null)
            {
                return Result.ok().message("查找用户举报的用户成功").data("UserInformations",UserInformations);
            }
            else{
                return Result.error().message("查找用户举报的用户失败");
            }
        }catch (Exception e){
            return Result.error().message("查找用户举报的用户失败");
        }
    }

    @Override
    public Result queryPostReport(int user_no){
        try {
            List<Post> posts=userReportMapper.getPostReport(user_no);
            if(posts!=null)
            {
                return Result.ok().message("查找用户举报的帖子成功").data("posts",posts).count(posts.size());
            }
            else{
                return Result.error().message("查找用户举报的帖子失败");
            }
        }catch (Exception e){
            return Result.error().message("查找用户举报的帖子失败");
        }
    }

    @Override
    public Result queryCommentReport(int user_no){
        try {
            List<Comment> comments=userReportMapper.getCommentReport(user_no);
            if(comments!=null)
            {
                return Result.ok().message("查找用户举报的评论成功").data("comments",comments);
            }
            else{
                return Result.error().message("查找用户举报的评论失败");
            }
        }catch (Exception e){
            return Result.error().message("查找用户举报的评论失败");
        }
    }

    @Override
    public int queryUserReported(int user_no){
        int count=userReportMapper.UserReported(user_no);
        return count;
    }

    @Override
    public int queryPostReported(int post_no){
        int count=userReportMapper.PostReported(post_no);
        return count;
    }

    @Override
    public int queryCommentReported(int comment_no){
        int count=userReportMapper.CommentReported(comment_no);
        return count;
    }

    @Override
    public Result queryUserReport(int no,int report_object_type){
        try {
            List<UserReport> userReports=userReportMapper.getUserReport(no,report_object_type);
            if(userReports!=null)
            {
                return Result.ok().message("查找举报信息成功").data("reports",userReports).count(userReports.size());
            }
            else{
                return Result.error().message("查找举报信息失败");

            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error().message("查找举报信息失败");
        }
    }

    @Override
    public Result dropTypeReport(int report_object_no, int report_object_type) {
        try {

            if(report_object_type==1)
            {
                userReportMapper.deleteTypeReport(report_object_no,report_object_type);
                return Result.ok().message("删除关联举报信息成功");
            }
            else if(report_object_type==2)
            {
                userReportMapper.deleteTypeReport(report_object_no,report_object_type);
                return Result.ok().message("删除关联举报信息成功");
            }
            else{
                return Result.error().message("删除关联举报信息失败");

            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error().message("删除关联举报信息失败");
        }
    }

    @Override
    public int countByDate(String date) {
        return userReportMapper.countByDate(date);
    }

    @Override
    public Result findAllReport() {
        try {
            List<UserReport> userReports=userReportMapper.findAllReport();
            if(userReports!=null)
            {
                return Result.ok().message("查找举报信息成功").data("reports",userReports).count(userReports.size());
            }
            else{
                return Result.error().message("查找举报信息失败");

            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error().message("查找举报信息失败");
        }
    }

    @Override
    public List<UserReport> findReportByReject() {
        return userReportMapper.findReportByReject();
    }

    @Override
    public Result queryAllCommunityReport(Integer communityNo) {
        try {
            List<Post> posts = postMapper.getCommunityPost(communityNo);
            List<UserReport> userReports = new ArrayList<>();
            for(Post post:posts){
                userReports.addAll(userReportMapper.getUserReport(post.getPostNo(),1));
            }
            if(userReports!=null)
            {
                return Result.ok().message("查找举报信息成功").data("reports",userReports).count(userReports.size());
            }
            else{
                return Result.error().message("查找举报信息失败");

            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error().message("查找举报信息失败");
        }
    }

    @Override
    public int updateRejectReasonByReportNo(Integer reportNo, String rejectReason) {
        return userReportMapper.updateRejectReasonByReportNo(reportNo, rejectReason);
    }
}
